package com.tg.app.helper;

import android.content.Context;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.alibaba.fastjson.JSONObject;
import com.appbase.custom.base.DeviceFeatureSettings;
import com.appbase.custom.constant.CameraConstants;
import com.tg.app.R;
import com.tange.module.device.feature.DeviceFeature;
import com.tange.base.toolkit.AppUtil;
import com.tange.base.toolkit.StringUtils;
import com.tg.app.view.QualityUIHelper;
import com.tg.appcommon.android.ResourcesUtil;
import com.tg.appcommon.android.TGLog;
import com.tg.data.bean.DeviceCapAIs;
import com.tg.data.bean.DeviceItem;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;


public class DeviceFeatureHelper {
    private static final String TAG = "DeviceFeatureHelper";
    public static boolean isSupportMicrophoneSetting(DeviceFeature deviceFeature){
        if (deviceFeature != null){
            if (deviceFeature.supportMicrophoneTune){
                return true;
            }
            if (deviceFeature.supportMicrophoneMuteable){
                return true;
            }

            if (StringUtils.equalsIgnoreCase(deviceFeature.microphoneInfo, CameraConstants.FEATURE_SUPPORT_YES)){
                return true;
            }

            if (!StringUtils.isEmpty(deviceFeature.microphoneInfo)){
                String[] list = deviceFeature.microphoneInfo.split(",");
                if (list.length > 0){
                    for (String s : list){
                        if (StringUtils.equalsIgnoreCase(s, DeviceFeature.FEATURE_SUPPORT_NO)){
                            return false;
                        }
                        if (StringUtils.equalsIgnoreCase(s, DeviceFeature.FEATURE_SUPPORT_YES)){
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    }

    public static boolean supportPTZ(DeviceFeature deviceFeature){
        if (deviceFeature != null && (deviceFeature.support2Lenses || deviceFeature.supportMultiChannels)){
            if (deviceFeature.supportMultiChannels){
                return deviceFeature.currentPlayer ==  0? deviceFeature.supportPTZ : deviceFeature.supportPTZ2;
            }

            if (deviceFeature.is2LensesCapZoom()){
                boolean isFar = deviceFeature.getZoomFeature(0).zoomDivider > 0 && deviceFeature.zoomPos >= deviceFeature.getZoomFeature(0).zoomDivider;
                return isFar? deviceFeature.supportPTZ : deviceFeature.supportPTZ2;
            }
            return deviceFeature.currentPlayer ==  0? deviceFeature.supportPTZ2 : deviceFeature.supportPTZ;

        }

        return  deviceFeature != null && deviceFeature.supportPTZ;
    }

    public static byte getRecordFun(DeviceFeature deviceFeature){
        if (deviceFeature != null){
            return deviceFeature.getRecordFun();
        }
        return 0;
    }

    public static boolean parseReset(@NonNull DeviceFeature deviceFeature){

        if (deviceFeature != null && (deviceFeature.support2Lenses || deviceFeature.supportMultiChannels)){
            if (deviceFeature.supportMultiChannels){
                return deviceFeature.currentPlayer ==  0? deviceFeature.ptzReset : deviceFeature.ptzReset2;
            }

            if (deviceFeature.is2LensesCapZoom()){
                boolean isFar = deviceFeature.getZoomFeature(0).zoomDivider > 0 && deviceFeature.zoomPos >= deviceFeature.getZoomFeature(0).zoomDivider;
                return isFar? deviceFeature.ptzReset : deviceFeature.ptzReset2;
            }
            return deviceFeature.currentPlayer ==  0? deviceFeature.ptzReset2 : deviceFeature.ptzReset;

        }


        return deviceFeature.ptzReset;
    }

    public static boolean supportPTZ(DeviceFeature deviceFeature, int pos){
        if (deviceFeature != null && (deviceFeature.support2Lenses || deviceFeature.supportMultiChannels)){
            return pos ==  0 ? deviceFeature.supportPTZ : deviceFeature.supportPTZ2;
        }
        return deviceFeature != null && deviceFeature.supportPTZ;
    }

    public static boolean horZOnly(DeviceFeature deviceFeature){
        return deviceFeature != null &&  deviceFeature.supportPTZ && deviceFeature.orientation == 0;
    }

    public static boolean vertOnly(DeviceFeature deviceFeature){
        return deviceFeature != null &&  deviceFeature.supportPTZ &&  deviceFeature.orientation == 1;
    }


    public static boolean horZOnly(DeviceFeature deviceFeature, int index){
        if (index == 0){
            return horZOnly(deviceFeature);
        }
        return deviceFeature != null && deviceFeature.supportPTZ2 && deviceFeature.orientation2 == 0;
    }

    public static boolean vertOnly(DeviceFeature deviceFeature, int index){
        if (index == 0){
            return vertOnly(deviceFeature);
        }
        return deviceFeature != null && deviceFeature.supportPTZ2 && deviceFeature.orientation2 == 1;
    }


    public static boolean isAec(DeviceFeature deviceFeature){
        return deviceFeature != null && deviceFeature.isAec;
    }
    public static boolean supportSpeaker(DeviceFeature deviceFeature){
        if (deviceFeature == null){
            return true;
        }
        return deviceFeature.supportSpeaker;
    }

    public static boolean supportMultiChannels(DeviceFeature mDeviceFeature){
        return mDeviceFeature != null && mDeviceFeature.supportMultiChannels;
    }

    public static boolean supportWatchPos(DeviceFeature mDeviceFeature){
        return mDeviceFeature != null && mDeviceFeature.watchPos ;
    }

    public static boolean supportPipVertical(DeviceFeature deviceFeature, Context context){
        return  DeviceFeatureHelper.supportMultiChannels(deviceFeature)&& AppUtil.isPipVerticalHeight(context);
    }
    public static boolean supportMultiChannelsPtz(DeviceFeature mDeviceFeature){
        return supportPTZ(mDeviceFeature)&&supportMultiChannels(mDeviceFeature);
    }
    public static boolean supportAiSwitch(DeviceFeature mDeviceFeature){
        return mDeviceFeature != null && mDeviceFeature.supportAiSwitch;
    }

    public static boolean supportTrackingCloseup(DeviceFeature mDeviceFeature){
        if (mDeviceFeature != null){
            DeviceCapAIs aIs =new DeviceCapAIs(mDeviceFeature.capAI);
            return aIs.isSupportTrackingCloseup();
        }
        return false;
    }

    public static boolean isSupportTumble(DeviceFeature mDeviceFeature){
        if (mDeviceFeature != null){
            DeviceCapAIs aIs = new  DeviceCapAIs(mDeviceFeature.capAI);
            return aIs.isSupportTumble();
        }
        return false;
    }


    public static boolean isSupportSittingPosition(DeviceFeature mDeviceFeature) {
        if (mDeviceFeature != null){
            DeviceCapAIs aIs =new  DeviceCapAIs(mDeviceFeature.capAI);
            return aIs.isSupportSittingPosition();
        }
        return false;
    }


    public static boolean supportPIR(DeviceFeature mDeviceFeature){
        return mDeviceFeature != null && mDeviceFeature.supportPIR;
    }

    public static boolean is2LensesCapZoom(DeviceFeature mDeviceFeature) {
        return mDeviceFeature != null && mDeviceFeature.is2LensesCapZoom();
    }

    public static boolean isNoMedia(DeviceFeature deviceFeature){
        return deviceFeature == null || deviceFeature.noMedia;
    }

    public static boolean isSupportCruise(DeviceFeature deviceFeature){
        return deviceFeature != null && deviceFeature.cruise;
    }

    public static float analogZoom(DeviceFeature mDeviceFeature,float zoom, int index){
        if (!is2LensesCapZoom(mDeviceFeature)){
            return zoom;
        }
        DeviceFeature.ZoomFeature zoomFeature = mDeviceFeature.getZoomFeature(index);
        if (!zoomFeature.isAnalogZoom(zoom)){
            return zoom;
        }
        if (zoomFeature.isInEndArea(zoom)){
          return   zoomFeature.zoomDivider;
        }
        return  0;
    }

    public static float analogZoom(DeviceFeature mDeviceFeature,float zoom){
        return analogZoom(mDeviceFeature, zoom, 0);
    }
    public static boolean hasMultiChannelsUpdate(DeviceFeature mDeviceFeature,DeviceFeature newFeature){
        return !supportMultiChannels(mDeviceFeature)&&supportMultiChannels(newFeature);
    }


    public static List<String> getResolution(DeviceFeature deviceFeature) {
        String[] newArr = ResourcesUtil.getResources().getStringArray(R.array.camera_live_resolution);
        List<String> list = Arrays.asList(newArr);
        Collections.reverse(list);
        if (deviceFeature != null && deviceFeature.getSupportResolutions() != null) {
            list = QualityUIHelper.getQualitys(deviceFeature);
        }
        List<String> data = new ArrayList<>();
        data.addAll(list);
        return data;
    }

    public static boolean supportFocus(DeviceFeature mDeviceFeature){
        return mDeviceFeature != null && mDeviceFeature.mfocus;
    }

    public static boolean supportTimeAction(DeviceFeature deviceFeature){
        return deviceFeature != null && deviceFeature.doubleLightTimer;
    }

    public static boolean supportLightDim(DeviceFeature deviceFeature){
        return deviceFeature != null && deviceFeature.doubleLightDim;
    }


    public static DeviceFeature getDeviceFeatureByDevice(DeviceItem deviceItem) {
        DeviceFeature deviceFeature = null;
        if (deviceItem != null && !TextUtils.isEmpty(deviceItem.abilities)) {
            TGLog.i(TAG, "[parseServerDeviceFeature] mDevice.abilities = " + deviceItem.abilities);
            final HashMap<String, String> map = new HashMap<>();
            try {
                JSONObject jsonObject = JSONObject.parseObject(deviceItem.abilities);
                if (jsonObject != null && jsonObject.keySet() != null) {
                    for (String key : jsonObject.keySet()) {
                        map.put(key, jsonObject.getString(key));
                    }
                }
                TGLog.i(TAG, "[parseServerDeviceFeature] device feature parse ret = " + jsonObject);
            } catch (Throwable ex) {
                TGLog.e(TAG, "[parseServerDeviceFeature] device feature parse error: " + ex.toString());
            }

            if (map.size() > 0) {
                TGLog.i(TAG, "[parseServerDeviceFeature] nap = " + map);
                DeviceFeatureSettings.DeviceFeatureSettingsResp deviceFeatureSettingsResp = new DeviceFeatureSettings.DeviceFeatureSettingsResp();
                deviceFeatureSettingsResp.uuid = deviceItem.uuid;
                deviceFeatureSettingsResp.feature = map;
                deviceFeature = DeviceFeature.newDeviceFeature(deviceFeatureSettingsResp);
            }

            TGLog.i(TAG, "[parseServerDeviceFeature] deviceFeature = " + deviceFeature);

            if (deviceFeature != null) {
                TGLog.i(TAG, "[parseServerDeviceFeature] deviceFeature.noMedia: " + deviceFeature.noMedia);
            }
        }
        return deviceFeature;
    }
}
